package net.bwie.realtime.vehicle2.job;

import com.alibaba.fastjson.JSON;
import net.bwie.realtime.jtp.common.utils.DateTimeUtil;
import net.bwie.realtime.jtp.common.utils.JdbcUtil;
import net.bwie.realtime.jtp.common.utils.KafkaUtil;
import net.bwie.realtime.vehicle2.bean.VehicleData;
import net.bwie.realtime.vehicle2.function.VehicleWindowBatteryHealthFunction;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.datastream.WindowedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.WindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.time.Duration;
import java.util.ArrayList;

/**
 * 平均车辆健康度
 * @Author: FuHe
 * @Date: 2025/6/7
 */
public class VehicleWindowBatteryHealthJob2 {
    public static void main(String[] args) throws Exception {
        // 设置执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        // 读取数据源 kafka
        DataStream<String> kafkaStream = KafkaUtil.consumerKafka(env, "vehicle-data-log");
        // 处理数据
        DataStream<String> statusStream = handle(kafkaStream);
        statusStream.print();
        // 写入数据 clickhouse
        JdbcUtil.sinkToClickhouseUpsert(statusStream,"INSERT INTO vehicle_data.vehicle_health (window_start, window_end, vin_health)\n" +
                "VALUES (?, ?, ?)");
        // 提交执行
        env.execute("VehicleWindowRealtimeStatus2");
    }

    private static DataStream<String> handle(DataStream<String> kafkaStream) {
        SingleOutputStreamOperator<VehicleData> map = kafkaStream.map(line -> JSON.parseObject(line, VehicleData.class));
        SingleOutputStreamOperator<VehicleData> vehicleDataSingleOutputStreamOperator = map.assignTimestampsAndWatermarks(WatermarkStrategy
                .<VehicleData>forBoundedOutOfOrderness(Duration.ofSeconds(0))
                .withTimestampAssigner(
                        new SerializableTimestampAssigner<VehicleData>() {
                            @Override
                            public long extractTimestamp(VehicleData element, long recordTimestamp) {
                                return element.getTimestamp();
                            }
                        }
                )
        );
        KeyedStream<VehicleData, String> vehicleDataStringKeyedStream = vehicleDataSingleOutputStreamOperator.keyBy(o -> "all");
        WindowedStream<VehicleData, String, TimeWindow> window = vehicleDataStringKeyedStream.window(
                TumblingEventTimeWindows.of(Time.minutes(1)));
        SingleOutputStreamOperator<String> apply = window.apply(new VehicleWindowBatteryHealthFunction());
        return apply;
    }
}